package com.example.androidexample.viewpager;

import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.TouchDelegate;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.androidexample.R;
import com.example.androidexample.viewpager.fragments.ViewPagerFragment1;
import com.example.androidexample.viewpager.fragments.ViewPagerFragment2;
import com.example.androidexample.viewpager.fragments.ViewPagerFragment3;
import com.example.basic.base.BaseActivity;

import java.util.ArrayList;

public class ViewPagerActivity extends BaseActivity {

    private static final String TAG = "ViewPagerActivity";

    private ViewPager viewpager1;
    private ViewPager viewpager2;
    private ViewPager viewpager3;


    private ArrayList<Fragment> fragments = new ArrayList<>();
    private ArrayList<String> titles = new ArrayList<>();
    private ArrayList<ImageView> imageViews = new ArrayList<>();
    private ArrayList<ImageView> imageViews1 = new ArrayList<>();
    private ArrayList<Integer> images = new ArrayList<>();

    private NormalFragmentViewPagerAdapter normalFragmentViewPagerAdapter;
    private CustomImageViewViewPagerAdapter customImageViewViewPagerAdapter;
    private CustomImageViewViewPagerAdapter customImageViewViewPagerAdapter1;

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        initFragments();
        initTitles();
        initImages();
        initImageViews();
        initImageViews1();

        initViewpager1();
        initViewpager2();
        initViewpager3();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initViewpager3() {

        FrameLayout container = findViewById(R.id.container);
        viewpager3 = findViewById(R.id.viewpager3);
        customImageViewViewPagerAdapter1 = new CustomImageViewViewPagerAdapter(imageViews1);
        viewpager3.setAdapter(customImageViewViewPagerAdapter1);
        viewpager3.setOffscreenPageLimit(4);
        viewpager3.post(new Runnable() {
            @Override
            public void run() {
                expendTouchArea(viewpager3,300);
            }
        });
        viewpager3.setPageTransformer(false,new ZoomOutPageTransformer());
        container.setOnTouchListener((v, event) -> viewpager3.dispatchTouchEvent(event));

    }

    private void initViewpager2() {
        viewpager2 = findViewById(R.id.viewpager2);
        customImageViewViewPagerAdapter = new CustomImageViewViewPagerAdapter(imageViews);
        viewpager2.setAdapter(customImageViewViewPagerAdapter);
        viewpager2.setOffscreenPageLimit(4);
        viewpager2.setPageMargin(100);
        viewpager2.post(new Runnable() {
            @Override
            public void run() {
                expendTouchArea(viewpager2,300);
            }
        });
        viewpager2.setPageTransformer(false,new MyTransformation());

    }

    private int index = 0;

    private void initImageViews1() {
        index = 0;
        for (Integer image : images) {
            ImageView imageView = new ImageView(this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setTag("tag_" + index);
            imageView.setImageDrawable(ContextCompat.getDrawable(this,image));
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(100,ViewGroup.LayoutParams.MATCH_PARENT);
            imageView.setLayoutParams(layoutParams);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i(TAG, "onClick: tag = " + v.getTag());
                }
            });
            imageViews1.add(imageView);
            index++;
        }
    }

    private void initImageViews() {
        for (Integer image : images) {
            ImageView imageView = new ImageView(this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageDrawable(ContextCompat.getDrawable(this,image));
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(100,ViewGroup.LayoutParams.MATCH_PARENT);
            imageView.setLayoutParams(layoutParams);
            imageViews.add(imageView);
        }
    }

    /**
     * 扩展点击区域的范围
     *
     * @param view       需要扩展的元素，此元素必需要有父级元素
     * @param expendSize 需要扩展的尺寸（以sp为单位的）
     */
    public void expendTouchArea(final View view, final int expendSize) {
        if (view != null) {
            final View parentView = (View) view.getParent();

            parentView.post(new Runnable() {
                @Override
                public void run() {
                    Rect rect = new Rect();
                    view.getHitRect(rect); //如果太早执行本函数，会获取rect失败，因为此时UI界面尚未开始绘制，无法获得正确的坐标
                    Log.d(TAG, "run: " + rect);
                    rect.left = 0;
//                    rect.top -= expendSize;
                    rect.right = getResources().getDisplayMetrics().widthPixels;
//                    rect.bottom += expendSize;
                    Log.d(TAG, "run: " + rect);
                    parentView.setTouchDelegate(new TouchDelegate(rect, view));
                }
            });
        }
    }

    private void initImages() {
        images.add(R.drawable.ic_test_img1);
        images.add(R.drawable.ic_test_img1);
        images.add(R.drawable.ic_test_img1);
        images.add(R.drawable.ic_test_img1);
        images.add(R.drawable.ic_test_img1);
        images.add(R.drawable.ic_test_img1);
    }

    private void initTitles() {
        titles.add("Fragment1");
        titles.add("Fragment2");
        titles.add("Fragment3");
    }

    private void initFragments() {
        fragments.add(new ViewPagerFragment1());
        fragments.add(new ViewPagerFragment2());
        fragments.add(new ViewPagerFragment3());
    }

    private void initViewpager1() {
        viewpager1 = findViewById(R.id.viewpager1);
        normalFragmentViewPagerAdapter = new NormalFragmentViewPagerAdapter(getSupportFragmentManager(),fragments,titles);
        viewpager1.setAdapter(normalFragmentViewPagerAdapter);
    }



    @Override
    protected int setLayoutXmlid() {
        return R.layout.activity_viewpager;
    }

    @Override
    public String getTag() {
        return "ViewPagerActivity";
    }

}
